package My.Nhom2.ServiceSearch;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import android.widget.AdapterView;
import android.widget.Toast;

import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * The Class ServiceSearchActivity.
 */
public class ServiceSearchActivity extends Activity implements OnClickListener {
	/** The retriever. */
	DataRetrieve retriever;

	/** The Constant COUNTRIES. */
	private static final String[] COUNTRIES = { "nha hang", "benh vien",
			"quan an", "karaoke", "ATM", "cho", "tram xang", "shop thoi trang",
			"bus", "rap chieu phim", "san van dong" };
	/** The btn_ok. */
	private Button btn_ok;

	/** The ac_txt. */
	private AutoCompleteTextView ac_txt;

	Spinner spr_sort;

	Spinner spi_typesort;

	/** The lv_ result. */
	private ListView lv_Result;

	/** The result. */
	ServiceSearchAdapter result;

	/** The list. */
	ArrayList<DataService> list = new ArrayList<DataService>();

	public static DataService node;
	private boolean flag = false;

	private String typesort;

	/*
	 * (non-Java doc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		retriever = new DataRetrieve();
		retriever.connect(this);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, COUNTRIES);

		ac_txt = (AutoCompleteTextView) findViewById(R.id.ac_search);
		ac_txt.setThreshold(1);
		ac_txt.setDropDownHeight(200);
		ac_txt.setAdapter(adapter);

		spr_sort = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter adapter_sort = ArrayAdapter.createFromResource(this,
				R.array.itemsort, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spr_sort.setAdapter(adapter_sort);
		final int currentitem = spr_sort.getSelectedItemPosition();
		spr_sort.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(result !=null)
				{
					if(arg2 ==0)
						result.sortAddress(typesort);
					if(arg2 == 1)
						result.sortNumberAddr(typesort);
					if(arg2 == 2)
						result.sortStreet(typesort);
					if(arg2 == 3)
						result.sortWard(typesort);
					if(arg2 == 4)
						result.sortDistrict(typesort);			
					lv_Result.setAdapter(result);		
				}
				

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		spi_typesort = (Spinner) findViewById(R.id.spi_typesort);
		ArrayAdapter adapter_typesort = ArrayAdapter.createFromResource(this,
				R.array.stypesort, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spi_typesort.setAdapter(adapter_typesort);
		typesort = (String) spi_typesort.getSelectedItem();
		spi_typesort.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(result !=null)
				{
					if(spr_sort.getSelectedItem()=="Dịch vụ")
						result.sortAddress(typesort);
					if(spr_sort.getSelectedItem()=="Số nhà")
						result.sortNumberAddr(typesort);
					if(spr_sort.getSelectedItem()=="Đường")
						result.sortStreet(typesort);
					if(spr_sort.getSelectedItem()=="Phường")
						result.sortWard(typesort);
					if(spr_sort.getSelectedItem()=="Quận")
						result.sortDistrict(typesort);			
					lv_Result.setAdapter(result);		
				}
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});

		btn_ok = (Button) findViewById(R.id.btn_search);
		btn_ok.setOnClickListener(this);

		lv_Result = (ListView) findViewById(R.id.listResult);

		lv_Result.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				node = (DataService) lv_Result.getItemAtPosition(position);
				view.getFocusables(position);
				view.setSelected(true);
				flag = true;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (flag) {
			switch (item.getItemId()) {
			case R.id.mn_map: {
				startActivity(new Intent(this, GoogleMapOfflineActivity.class));
				return true;
			}
			case R.id.mn_price: {
				ArrayList<ItemMenu> _list = new ArrayList<ItemMenu>();		
				_list = retriever.getMenuList(node.maDuLieu);
				Toast _toast;
				if(_list.isEmpty())
				{
					_toast = Toast.makeText(getApplicationContext(),
							"Không tìm thấy", Toast.LENGTH_LONG);
					_toast.show();
				}
				else
				startActivity(new Intent(this, MenuListActivity.class));
				return true;
			}
			case R.id.mn_googlemap:	
				startActivity(new Intent(this, GoogleMapOnlineActivity.class));
				return true;
			case R.id.mn_exit:
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}
		return super.onOptionsItemSelected(item);
	}
	public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
			long arg3) {
		// TODO Auto-generated method stub
			
	}
	
	/*
	 * (non-Java doc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.btn_search) {
			list.clear();
			String s = ac_txt.getText().toString();
			list = retriever.getDataVer2(s);
			if (list.isEmpty()) {
				Toast toast = Toast.makeText(getApplicationContext(),
						"Không tìm thấy", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER_HORIZONTAL, -100, -100);
				toast.show();
			} else {
				result = new ServiceSearchAdapter(this, list);
				result.sortAddress(typesort);
				lv_Result.setAdapter(result);
			}
		}
	}
	
}